/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.kit.conversation.wallet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.entry.RedRecordResult;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.UserInfo;

public class RedRecordAdapter extends RecyclerView.Adapter<RedRecordAdapter.RedRecordViewHolder> {
    public List<RedRecordResult.RecordEntry> redRecords;
    private UserViewModel userViewModel;
    private PackgeRecordActivity activity;

    public RedRecordAdapter(UserViewModel userViewModel){
        this.userViewModel = userViewModel;
    }
    public void setRedRecords(List<RedRecordResult.RecordEntry> redRecords) {
        this.redRecords = redRecords;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RedRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_red_record, parent, false);
        return new RedRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RedRecordViewHolder holder, int position) {
        holder.bindRecord(redRecords.get(position));
    }

    @Override
    public int getItemCount() {
        if (redRecords == null) {
            return 0;
        }
        int count = redRecords.size();
        return count;
    }

    class RedRecordViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tvNick)
        TextView tvNick;
        @BindView(R2.id.tvTime)
        TextView tvTime;
        @BindView(R2.id.tvBalance)
        TextView tvBalance;

        public RedRecordViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindRecord(RedRecordResult.RecordEntry recordEntry) {
//            userViewModel.getUserInfoAsync(recordEntry.getId(), true)
//                    .observe((LifecycleOwner) activity.getLifecycle(), info -> {
//                        UserInfo userInfo = info;
//                        if (userInfo != null) {
//                            tvNick.setText(userInfo.displayName);
//                        }
//                    });
            tvNick.setText(recordEntry.getDisplayName());
            tvTime.setText(recordEntry.getCreateTime());
            tvBalance.setText(recordEntry.getAmount());
        }

    }
}
